// module user {

//     export class UserSetting extends egret.EventDispatcher {

//         public static readonly SettingStoreKey: string = "user_setting_game_data_key";

//         public static readonly SYNC_COIN: string = "sync_coin_data";


//         public static readonly SYNC_MONEY: string = "sync_money_data";

//         public maxScore: number = 0;

//         public constructor() {
//             super();
//             egret.log("创建UserSetting");
//             this.restore();
//         }

//         public getScoreData(): Array<any> {
//             let normalData = {
//                 "key": "score",
//                 "value": this.maxScore.toString(),
//             };
            
//             let scoreData = [normalData];
//             return scoreData;
//         }

//         public onlyCommitScore(): void {
//             let scoreData = this.getScoreData();
//             let openDataContext = wx.getOpenDataContext();
//             openDataContext.postMessage({
//                 command: page.PkFriendPage.Only_Submit_Score,
//                 scoreData: scoreData,
//             });
//         }

//         private restore(): void {
//             if (manager.GameManager.isProduct) {
//                 let userSettingInfo: any = wx.getStorageSync(UserSetting.SettingStoreKey);
//                 egret.log("恢复用户数据:", userSettingInfo);
//                 if (userSettingInfo != null && userSettingInfo["playTime"] != null) {
//                     try {
//                         this.maxScore = userSettingInfo.maxScore;

//                         egret.log("历史数据已恢复,无异常");
//                     } catch (e) {
//                         egret.log("历史数据已恢复,无异常");
//                     }
//                 }
//             }
//         }


//         public saveMaxScore(score: number): void {
//             egret.log("保存游戏分数 begin");
//             if (this.maxScore < score) {
//                 this.maxScore = score;
//                 this.save();
//                 egret.log("保存游戏分数 end");
//             }
//         }

//         public save(): void {
//             if (manager.GameManager.isProduct) {
//                 wx.setStorageSync(user.UserSetting.SettingStoreKey,
//                     {
//                         maxScore: this.maxScore,
//                     });
//             }
//         }
//     }

// }



module user {

    export class UserSetting {

        public playTime: number = 0;

        public maxScore: number = 0;

        public reliveTimes: number = 0;

        public lastDay:number = 0;

        static readonly SettingStoreKey: string = "user_setting_game_data_key";

        public constructor() {
            egret.log("创建UserSetting");

            this.restore();
        }

        private restore(): void {
            if (manager.GameManager.isProduct) {
                let userSettingInfo: any = wx.getStorageSync(UserSetting.SettingStoreKey);
                egret.log("恢复用户数据:", userSettingInfo);
                // if (userSettingInfo != null && userSettingInfo["playTime"] != null) {
                    try {
                        this.maxScore = userSettingInfo.maxScore || 0;
                    } catch (e) {

                    }
                // }
            }
        }

        public checkRelive(): boolean {
            return this.reliveTimes < manager.GameManager.getInstance().getUserManager().getBagManager().getReliveCount();
        }

        public relive(): void {
            this.reliveTimes++;
            this.save();
        }

        public saveMaxScore(score: number): void {
            egret.log("保存游戏分数 begin");
            if (this.maxScore < score) {
                this.maxScore = score;
                this.save();
                egret.log("保存游戏分数 end");
            }
        }

        // public saveMaxLevel(score: number): void {
        //     egret.log("保存闯关数 begin");
        //     if (this.maxScore < score) {
        //         this.maxScore = score;
        //         this.save();
        //         egret.log("保存游戏分数 end");
        //     }
        // }

        public save(): void {
            if (manager.GameManager.isProduct) {
                wx.setStorageSync(user.UserSetting.SettingStoreKey,
                    {
                        maxScore: this.maxScore,
                    });
            }
            console.log('保存最高分完成')
            console.log('保存最高分完成' + wx.getStorageSync(user.UserSetting.SettingStoreKey).maxScore)
        }

    }

}